package com.hospital.data.simulation.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.pojo.TDoctorInfo;
import com.hospital.data.simulation.exception.DoctorInfoServiceException;
import com.hospital.data.simulation.exception.enums.DoctorInfoServiceExpCode;
import com.hospital.data.simulation.mapper.TDoctorInfoMapper;
import com.hospital.data.simulation.service.TDoctorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class TDoctorInfoServiceImpl extends ServiceImpl<TDoctorInfoMapper, TDoctorInfo> implements TDoctorInfoService{

    @Autowired
    private TDoctorInfoMapper tDoctorInfoMapper;
    @Override
    public Long addDoctorInfo(TDoctorInfo doctorInfo) {
        if(tDoctorInfoMapper.insert(doctorInfo)==0){
            throw new DoctorInfoServiceException(DoctorInfoServiceExpCode.ADD_DOCTOR_INFO_FAILED);
        }
        return doctorInfo.getId();
    }

    @Override
    public void deleteDoctorInfo(Long id) {
        if (tDoctorInfoMapper.deleteById(id)==0){
            throw new DoctorInfoServiceException(DoctorInfoServiceExpCode.DELETE_DOCTOR_INFO_FAILED);
        }
    }

    @Override
    public void updateDoctorInfo(TDoctorInfo doctorInfo) {
        LambdaUpdateWrapper<TDoctorInfo> wrapper = new LambdaUpdateWrapper<>();
        constructWrapper(wrapper,doctorInfo);
        if(tDoctorInfoMapper.update(wrapper)==0){
            throw new DoctorInfoServiceException(DoctorInfoServiceExpCode.UPDATE_DOCTOR_INFO_FAILED);
        }
    }
    private void constructWrapper(LambdaUpdateWrapper<TDoctorInfo> wrapper,TDoctorInfo doctorInfo){
        wrapper.eq(TDoctorInfo::getId,doctorInfo.getId());
        if(!ObjectUtils.isEmpty(doctorInfo.getDepartment())){
            wrapper.set(TDoctorInfo::getDepartment,doctorInfo.getDepartment());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getDepartmentId())){
            wrapper.set(TDoctorInfo::getDepartmentId,doctorInfo.getDepartmentId());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getEducationLevel())){
            wrapper.set(TDoctorInfo::getEducationLevel,doctorInfo.getEducationLevel());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getGraduatedSchool())){
            wrapper.set(TDoctorInfo::getGraduatedSchool,doctorInfo.getGraduatedSchool());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getOfficeAddress())){
            wrapper.set(TDoctorInfo::getOfficeAddress,doctorInfo.getOfficeAddress());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getPhotoDirPath())){
            wrapper.set(TDoctorInfo::getPhotoDirPath,doctorInfo.getPhotoDirPath());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getPhotoFile())){
            wrapper.set(TDoctorInfo::getPhotoFile,doctorInfo.getPhotoFile());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getRegistrationFee())){
            wrapper.set(TDoctorInfo::getRegistrationFee,doctorInfo.getRegistrationFee());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getScopeList())){
            wrapper.set(TDoctorInfo::getScopeList,doctorInfo.getScopeList());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getTitle())){
            wrapper.set(TDoctorInfo::getTitle,doctorInfo.getTitle());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getTitleId())){
            wrapper.set(TDoctorInfo::getTitleId,doctorInfo.getTitleId());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getYearsOfExperience())){
            wrapper.set(TDoctorInfo::getYearsOfExperience,doctorInfo.getYearsOfExperience());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getSpecialty())){
            wrapper.set(TDoctorInfo::getSpecialty,doctorInfo.getSpecialty());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getQualificationNumber())){
            wrapper.set(TDoctorInfo::getQualificationNumber,doctorInfo.getQualificationNumber());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getQualificationType())){
            wrapper.set(TDoctorInfo::getQualificationType,doctorInfo.getQualificationType());
        }
        if(!ObjectUtils.isEmpty(doctorInfo.getQualificationTypeId())){
            wrapper.set(TDoctorInfo::getQualificationTypeId,doctorInfo.getQualificationTypeId());
        }
    }
}
